<?php
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use changpay\ChangPay;
class Bank extends Controller
{
//	银行卡绑定接收验证码(鉴权请求)
    public function bank1()
    {
    	$post = input('post.');
        $session = Session::get('name');
        $user = Db::table('xb_mem_info')->where('UserName',$session)->find();
        $userId = $user['ID'];
    	$changpay= new \changpay\ChangPay;
		$data=[];
		$insert['UserID']=$data['MerUserId']=$userId ; //用户标识 //用户ID 作为唯一标识
		$insert['BankNo']= $data['BkAcctNo'] =$post['bank_numer']; //卡号
		$data['IDNo'] =   $post['ID']; //证件号
		$data['CstmrNm'] =  $post['user_name'] ; //持卡人姓名
		$insert['YMobile'] = $data['MobNo'] = $post['mobile'] ; //银行预留手机号	
		$insert['BankName'] =$post['bank_name'];//开户银行名称
		$insert['IsDel'] =0;
		$insert['RenzTime'] = date('Y-m-d H:i:s');
//		$data['PartnerId'] ='200002080023' ; //商户号码
        $data['PartnerId']=Db::table('xb_sys_inteparameter')->where('ParaName','changpay')->value('ParaValue');
		$datas=$changpay->bank1($data);
		
		// [TrxId] => 201902114093933300  把$json中的这个参数返回   然后再用这个数据去请求下一个接口
		if($datas->AcceptStatus == "S"){
			$ret =Db::table('xb_renzen_bank')->where('UserID',$userId)->find();
			if(!$ret){
				$result = Db::table('xb_renzen_bank')->insert($insert);//保存银行卡信息
				if($result){
					return json_encode(array('status'=>1,'data'=>$datas));
				}else{
					return json_encode(array('status'=>0,'data'=>'认证失败'));
				}
			}
		}else{
			return json_encode(array('status'=>0,'data'=>$datas));
		}
		
    }


	//	银行卡绑定操作（鉴权确定）
    public function bank2()
    {
    	$post = input('post.');
		$TrxId = input('TrxId');
		$SmsCode = input('verify');//验证码
        $session = Session::get('name');
        $user = Db::table('xb_mem_info')->where('UserName',$session)->find();
        $userId = $user['ID'];
		$changpay= new \changpay\ChangPay;
		//$data['OriAuthTrxId'] =$data['TrxId']; //填写需要退款的原始订单号  //鉴权请求 的外部流水号码   TrxId
        $data['OriAuthTrxId'] =$TrxId; //填写需要退款的原始订单号  //鉴权请求 的外部流水号码   TrxId
		$data['SmsCode'] = $SmsCode ; //鉴权短信验证码
//		$data['PartnerId'] = '200002080023' ; //商户号码
		$data['PartnerId']=Db::table('xb_sys_inteparameter')->where('ParaName','changpay')->value('ParaValue');
		$datas=$changpay->bank2($data);
		if($datas->AcceptStatus == 'S'){//绑卡成功
			$update['isBank'] = 1;
			$result = Db::table('xb_mem_info')->where('ID',$userId)->update($update);
			$ret = Db::table('xb_renzen_bank')->where('UserID',$userId)->update(array('Status'=>1));
			if($result && $ret){
				return json_encode(array('status'=>1,'data'=>$datas));
			}else{
				return json_encode(array('status'=>0,'data'=>$datas));
			}
		}else{
			return json_encode(array('status'=>0,'data'=>$datas));
		}
    }
	//用户支付 \H5支付
	public function pay(){
    	$post = input('post.');
        $session = Session::get('name');
        $user = Db::table('xb_mem_info')->where('UserName',$session)->find();
        $userId = $user['ID'];
		$changpay= new \changpay\ChangPay;
//		$data['PartnerId'] = '200002080023'; //商户号码
		$data['PartnerId']=Db::table('xb_sys_inteparameter')->where('ParaName','changpay')->value('ParaValue');
		$data['userId'] =$userId;//用户标志
		$data['BankNo'] = $post['BankNo'];
		$data['ID']= $post['Id'];
		$data['name']= $post['name'];
		$data['mobile']= $post['mobile'];
		$data['OrderSn'] =$post['OrderSn'];
		$data['Money'] =$post['Money'];
		$data['Type']=$post['Type'];
		$datas = $changpay->pay($data);
		if($datas){
			return json_encode(array('status'=>1,'data'=>$datas));
		}else{
			return json_encode(array('status'=>0,'data'=>$datas));
		}
	}
}
